<template>

    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
            <el-form-item label="所属行业" prop="industryId">
                <div class="w-[200px]">
                    <el-select class="flex-1" v-model="queryParams.industryId" placeholder="请选择所属行业" clearable>
                        <el-option class="flex-1" v-for="item in industryList" :key="item.industryId" :label="item.name"
                            :value="item.industryId" />
                    </el-select>
                </div>
            </el-form-item>
            <el-form-item label="所属系统" prop="systemName">
                <el-input style="width: 200px;" v-model="queryParams.systemName" placeholder="请输入所属系统" clearable
                    @keyup.enter="handleQuery" />
            </el-form-item>


            <el-form-item label="商家名称" prop="name">
                <el-input v-model="queryParams.name" placeholder="请输入商家标题" clearable style="width: 200px"
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>


        <el-table v-loading="loading" :data="noticeList" @selection-change="handleSelectionChange">
            <!-- <el-table-column type="selection" width="55" align="center" show-overflow-tooltip /> -->
            <el-table-column label="序号" align="center" type="index" width="55">
                <template #default="{ $index }">
                    {{ (queryParams.currentPage - 1) * queryParams.pageSize + $index + 1 }}
                </template>
            </el-table-column>
            <el-table-column label="商家名称" align="center" prop="name" />
            <el-table-column label="财务监管总汇" v-if="typeName == '财务监管'" align="center" width="150"
                class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button link type="primary" icon="ZoomIn" @click="handleDetail(scope.row)">财务监管总汇</el-button>
                </template>
            </el-table-column>
            <el-table-column label="支付项目" v-if="typeName == '支付监管'" align="center" width="150"
                class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button link type="primary" icon="ZoomIn" @click="handleDetail(scope.row)">支付项目</el-button>
                </template>
            </el-table-column>
            <el-table-column label="经营监管总汇" v-if="typeName == '经营监管'" align="center" width="150"
                class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button link type="primary" icon="ZoomIn" @click="handleDetail(scope.row)">经营监管总汇</el-button>
                </template>
            </el-table-column>

            <el-table-column label="所属行业" align="center" prop="industryName" />
            <el-table-column label="所属系统" align="center" prop="systemName" show-overflow-tooltip />
            <el-table-column label="单位地址" align="center" prop="addressDetail" show-overflow-tooltip />
            <el-table-column label="法人姓名" align="center" prop="legalPersonName" />
            <el-table-column label="法人电话" align="center" prop="legalPersonPhone" />
            <el-table-column label="单位电话" align="center" width="120" show-overflow-tooltip prop="officeSpacePhone" />


            <el-table-column label="创建时间" align="center" prop="createTime" />
            <el-table-column label="状态" align="center" prop="isClose">
                <template #default="scope">
                    <span v-if="!scope.row.isClose">正常</span>
                    <span v-if="scope.row.isClose">停业</span>
                </template>
            </el-table-column>
            <el-table-column label="操作人员" align="center" prop="updateUserName" />


        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.currentPage"
            v-model:limit="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改商家对话框 -->
        <el-dialog :title="titleName" center v-model="open" width="780px" append-to-body>
            <el-form ref="noticeRef" :model="form" :formDisable="formDisable" :rules="rules" label-width="140px">
                <el-row>

                    <!-- 财务管理 -->
                    <template v-if="typeName == '财务监管'">
                        <el-col :span="24">
                            <el-form-item label="项目名称" prop="position">
                                <el-select v-model="form.position" placeholder="请选择项目名称">
                                    <el-option v-for="dict in arr1" :key="dict.prop" :label="dict.label"
                                        :value="dict.prop"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="今日报表" prop="salesRevenueMgmt">
                                <el-input v-model="form.salesRevenueMgmt" placeholder="请输入今日报表" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="本月报表" prop="salesRevenueMgmt">
                                <el-input v-model="form.salesRevenueMgmt" placeholder="请输入本月报表" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="昨日报表" prop="salesRevenueMgmt">
                                <el-input v-model="form.salesRevenueMgmt" placeholder="请输入昨日报表" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="本季度报表" prop="salesRevenueMgmt">
                                <el-input v-model="form.salesRevenueMgmt" placeholder="本季度报表" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="本周报表" prop="salesRevenueMgmt">
                                <el-input v-model="form.salesRevenueMgmt" placeholder="请输入本周报表" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="本年度报表" prop="salesRevenueMgmt">
                                <el-input v-model="form.salesRevenueMgmt" placeholder="请输入本年度报表" />
                            </el-form-item>
                        </el-col>
                    </template>



                    <template v-if="typeName == '经营监管'">
                        <el-col :span="24">
                            <el-form-item label="项目名称" prop="position">
                                <el-select v-model="form.position" placeholder="请选择项目名称">
                                    <el-option v-for="dict in arr2" :key="dict.prop" :label="dict.label"
                                        :value="dict.prop"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="今日报表" prop="salesRevenueMgmt">
                                <el-input v-model="form.salesRevenueMgmt" placeholder="请输入今日报表" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="本月报表" prop="salesRevenueMgmt">
                                <el-input v-model="form.salesRevenueMgmt" placeholder="请输入本月报表" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="昨日报表" prop="salesRevenueMgmt">
                                <el-input v-model="form.salesRevenueMgmt" placeholder="请输入昨日报表" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="本季度报表" prop="salesRevenueMgmt">
                                <el-input v-model="form.salesRevenueMgmt" placeholder="本季度报表" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="本周报表" prop="salesRevenueMgmt">
                                <el-input v-model="form.salesRevenueMgmt" placeholder="请输入本周报表" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="本年度报表" prop="salesRevenueMgmt">
                                <el-input v-model="form.salesRevenueMgmt" placeholder="请输入本年度报表" />
                            </el-form-item>
                        </el-col>
                    </template>

                    <!-- 经营监管 -->

                </el-row>
            </el-form>
            <!--  -->
            <!-- <template #footer>
          <div class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </template> -->
        </el-dialog>

    </div>
</template>

<script setup name="Notice">


const arr1 = [
    { 'label': '销售收入统计', 'prop': 'salesRevenue' },
    { 'label': '营收收入统计', 'prop': 'revenueIncome' },
    { 'label': '毛利收入统计', 'prop': 'grossProfit' },
    { 'label': '应收收入统计', 'prop': 'receivablesIncome' },
    { 'label': '欠收收入统计', 'prop': 'uncollectedIncome' },
    { 'label': '欠款预警统计', 'prop': 'arrearsWarning' },
    { 'label': '缺货预警统计', 'prop': 'stockOutWarning' },
    { 'label': '商品订单统计', 'prop': 'productOrders' },
    { 'label': '商品退单统计', 'prop': 'productReturns' },
    { 'label': '物料采购统计', 'prop': 'materialPurchases' },
    { 'label': '员工薪资统计', 'prop': 'employeeSalaries' },
    { 'label': '日常费用统计', 'prop': 'dailyExpenses' },
    { 'label': '正常损耗统计', 'prop': 'normalLoss' },
    { 'label': '销售收入统计', 'prop': 'salesRevenueMgmt' },
    { 'label': '营收收入统计', 'prop': 'revenueIncomeMgmt' },
    { 'label': '应收收入统计', 'prop': 'receivablesIncomeMgmt' },
    { 'label': '欠收收入统计', 'prop': 'uncollectedIncomeMgmt' },
    { 'label': '欠款预警统计', 'prop': 'arrearsWarningMgmt' },
    { 'label': '缺货预警统计', 'prop': 'stockOutWarningMgmt' },
    { 'label': '商品订单统计', 'prop': 'productOrdersMgmt' },
    { 'label': '商品退单统计', 'prop': 'productReturnsMgmt' }
]

const arr2 = [
    {'label': '销售收入统计', 'prop': 'salesRevenue'},
    {'label': '营收收入统计', 'prop': 'revenueIncome'},
    {'label': '应收收入统计', 'prop': 'receivablesIncome'},
    {'label': '欠收收入统计', 'prop': 'uncollectedIncome'},
    {'label': '欠款预警统计', 'prop': 'arrearsWarning'},
    {'label': '缺货预警统计', 'prop': 'stockOutWarning'},
    {'label': '商品订单统计', 'prop': 'productOrders'},
    {'label': '商品退单统计', 'prop': 'productReturns'}
]


import { useRoute } from 'vue-router';
const route = useRoute()

let typeName = ref('')
const type = ref(1)
const titleName = ref('')
watch(route, () => {
    if (route.meta.title == '财务监管') {
        typeName.value = '财务监管'
        type.value = 1
        titleName.value = '财务监管详情'
    } else if (route.meta.title == '支付监管') {
        typeName.value = '支付监管'
        type.value = 2
        titleName.value = '支付监管详情'
    } else if (route.meta.title == '经营监管') {
        typeName.value = '经营监管'
        type.value = 3
        titleName.value = '经营监管详情'
    }
}, { immediate: true })

import {
    authApiIndustryList
} from "@/api/login.js";

const industryList = ref([])
function authApiIndustryListApi() {
    authApiIndustryList().then(
        (res) => {
            industryList.value = [...res];
        }
    );
}

onMounted(() => {
    authApiIndustryListApi()
})

import {
    financeApimerchantlist,
    financeApibilllist
} from "@/api/backFinance/backFinance.js";

import {
    listProduct,
    productDelete,
    productUpdate,
    productDetail,
    productAdd,

} from "@/api/backProduct/backProduct";
const { proxy } = getCurrentInstance();

const noticeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

let im = ref("")

const data = reactive({
    form: {},
    queryParams: {
        currentPage: 1,
        pageSize: 10,
        name: undefined,
    },
    rules: {
        // name: [
        //   { required: true, message: "商家名称不能为空", trigger: "blur" },
        // ],
        // price: [
        //   { required: true, message: "商家价格不能为空", trigger: "blur" },
        // ],
        // stock: [
        //   { required: true, message: "商家库存不能为空", trigger: "blur" },
        // ],
        // imageList: [
        //   { required: true, message: "图片不能为空", trigger: "change" },
        // ],
    },
});

const { queryParams, form, rules } = toRefs(data);


// 禁用所有表单
const formDisable = ref(false)
/**详情按钮操作 */
function handleDetail(row) {
    reset();
    const merchantId = row.merchantId || ids.value;
    financeApibilllist(merchantId).then((response) => {
        form.value = response;
        open.value = true;
        title.value = "财务监管详情";
    });
    formDisable.value = true
}

/** 查询商家列表 */
function getList() {
    loading.value = true;
    if (typeName.value == '支付监管') {
        financeApibilllist(queryParams.value).then((response) => {
            noticeList.value = response.records;
            total.value = response.total;
            loading.value = false;
        });
    } else if (typeName.value == '财务监管' || typeName.value == '经营监管') {
        financeApimerchantlist(queryParams.value).then((response) => {
            noticeList.value = response.records;
            total.value = response.total;
            loading.value = false;
        });
    }

}

/** 取消按钮 */
function cancel() {
    open.value = false;
    reset();
}

/** 表单重置 */
function reset() {
    form.value = {
        "agentPrice": 0,
        "attention": "",
        "batchPrice": 0,
        "checkCert": "",
        "code": "",
        "codeImage": "",
        "component": "",
        "dosage": "",
        "effect": "",
        "expireDays": 0,
        "huiPrice": 0,
        "imageList": [],
        "materialCode": "",
        "model": "",
        "name": "",
        "place": "",
        "price": 0,
        "produceMerchantName": "",
        "produceProcessName": "",
        "productCategoryTypeId": 1763848515436650498,
        "purchasePrice": 0,
        "qualityCert": "",
        "replenishPrice": 0,
        "sortNum": "",
        "spec": "",
        "stock": "",
        "storageMethod": "",
        "treatPeriod": "",
        "ullage": "",
        "usage": "",
        "vipPrice": 0
    };
    proxy.resetForm("noticeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.currentPage = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
    ids.value = selection.map((item) => item.productId);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
    reset();
    open.value = true;
    title.value = "添加商家";
    form.productCategoryTypeId = 1763848515436650498
    return;
    productDetail().then((response) => {
        console.log(response, "response");
        form.value = { ...form.value, ...response };

    });

}

/**修改按钮操作 */
function handleUpdate(row) {
    reset();
    const productId = row.productId || ids.value;
    productDetail(productId).then((response) => {
        form.value = { ...form.value, ...response };
        open.value = true;
        title.value = "修改商家";
    });
    formDisable.value = false
}

/** 提交按钮 */
function submitForm() {
    proxy.$refs["noticeRef"].validate((valid) => {
        if (valid) {
            if (form.value.productId != undefined) {
                productUpdate(form.value).then((response) => {
                    proxy.$modal.msgSuccess("修改成功");
                    open.value = false;
                    getList();
                });
            } else {
                productAdd(form.value).then((response) => {
                    proxy.$modal.msgSuccess("新增成功");
                    open.value = false;
                    getList();
                });
            }
        }
    });
}

/** 删除按钮操作 */
function handleDelete(row) {
    const productId = row.productId || ids.value;
    proxy.$modal
        .confirm('是否确认删除商家标题为"' + productId + '"的数据项？')
        .then(function () {
            return productDelete(productId);
        })
        .then(() => {
            getList();
            proxy.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
}

getList();
</script>


<style scoped>
.el-image {
    width: 200px;
    height: 60px;
}
</style>